package com.mangguo.tingjian.content.mapper;

import com.mangguo.tingjian.content.dto.ContentCountResult;
import com.mangguo.tingjian.content.dto.CountResult;
import com.mangguo.tingjian.content.entity.DwUserFavorites;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 点赞表-同步 Mapper 接口
 * </p>
 *
 * @author auto
 * @since 2019-11-29
 */
@Mapper
public interface DwUserFavoritesMapper extends BaseMapper<DwUserFavorites> {
    @Select("select count(1) from dw_user_favorites where mdate >= #{start} and mdate <= #{end} and cid=#{contentId} and type=#{contentType}")
    Long countFavoritesByContentIdBetweenDate(@Param("start")Integer start,
                                              @Param("end") Integer end,
                                              @Param("contentId") Long contentId,
                                              @Param("contentType") Integer contentType);

    @Select("select count(1) as count, mdate from dw_user_favorites where mdate >= #{start} and mdate <= #{end} and cid=#{contentId} and type=#{contentType} group by mdate")
    List<CountResult> countFavoritesByContentIdBetweenDateGroupByDate(@Param("start")Integer start,
                                                                      @Param("end") Integer end,
                                                                      @Param("contentId") Long contentId,
                                                                      @Param("contentType") Integer contentType);

    @Select("<script>select count(1) as count,mdate,cid as contentId" +
            " from dw_user_favorites where mdate &gt;= #{start} and mdate &lt;= #{end} and " +
            "<if test='contentIds != null and contentIds.size()>0'> "+
            "cid IN " +
            "<foreach item='contentId' collection='contentIds' open='(' close=')' separator=','> "+
            "#{contentId}"+
            "</foreach> AND "+
            "</if>"+
            "type = #{contentType} " +
            "group by mdate,cid" +
            "</script>")
    List<ContentCountResult> countFavoritesByContentIdBetweenDateGroupByDates(@Param("start")Integer start,
                                                                               @Param("end")Integer end,
                                                                               @Param("contentIds") List<Long> contentIds,
                                                                             @Param("contentType") Integer contentType);

}
